package com.welearn.xdstudentmanagement.controller;

import com.welearn.xdstudentmanagement.common.Result;
import com.welearn.xdstudentmanagement.entity.Admin;
import com.welearn.xdstudentmanagement.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/admin")
public class AdminController {
  @Autowired
  private AdminService adminService;

  @RequestMapping(value = "/login", method = RequestMethod.POST)
  public Result login(@RequestBody Admin admin) {
    List<Admin> listAdmin = adminService.selectAdmin(admin.getName(), admin.getPwd());
    if (listAdmin.isEmpty()) {
      return new Result(200, "登录失败!", new HashMap<>());
    }
    Map<String, List<Admin>> data = new HashMap<>();
    data.put("admin", listAdmin);
    return new Result(200, "登录成功", data);
  }
  @RequestMapping(value = "/getall", method = RequestMethod.POST)
  public Result getall(){
    Map<String, Object> data = new ManagedMap<>();
    if (adminService.selectAllAdmin().isEmpty()) {
      return new Result(200, "查看失败", new HashMap<>());
    }
    data.put("adminlist",adminService.selectAllAdmin().toArray());

    return new Result(200, "查看成功", data);
  }
}
